<template>
  <view>
    <backGroundNoImg :titleName="'问题反馈'" :isShowBack="true"></backGroundNoImg>
    <view class="question_container">
      <view class="textarea_con">
        <textarea 
          class="textarea" 
          placeholder="请描述下您的问题"
          v-model="questionText"
          @input="countNumFn"
          maxlength="500"
          ></textarea>
        <text class="count">
          <text class="orange_color">{{questionText.length}}</text>
          <text>/500</text>
        </text>
      </view>

      <view class="uploadPic">
        <text>上传图片</text> <text>（最多三张）</text>
      </view>
      <view class="upload_container mt10">
        <view class="pic_box upload_pic_con" v-for="(url,index) in picList" :key="index">
          <image class="upload_pic" :src="url" />
          <view class="del_btn" @click="delPicFn(index)">删除</view>
        </view>
        <view v-show="picList.length<3" @click="addPicFn" class="pic_box click_upload_btn">点击上传</view>
      </view>
      <button :disabled="disabled" :style="[{'opacity':(disabled?'0.5':'1')}]" @click="submitFn"  class="full_width_main_btn">提交</button>
    </view>
  </view>
</template>
  
<script setup>
import Taro from '@tarojs/taro';
import backGroundNoImg from '../../components/backGroundNoImg.vue';
import { ref, reactive } from 'vue'
import requset from '../../network/request';
import config from '../../network/baseUrl'
import { getCurrentInstance } from 'vue';
const instance = getCurrentInstance();
instance.appContext.config.globalProperties.$loadFontFace()

Taro.setNavigationBarColor({
  frontColor: "#000000",
  backgroundColor: '#F7F3E8'
})

const userId = Taro.getStorageSync('userId');
const userToken = Taro.getStorageSync('token');

let questionText = ref('')
let picList = ref([])
let disabled = ref(true)
let picArr = []

function countNumFn() {
  watchDisable()
}

// 删除图片
function delPicFn(index) {
  picList.value.splice(index, 1);
  picArr.splice(index, 1);
  watchDisable()
  // console.log('删除后的arr：：', picArr)
}

// 添加图片
function addPicFn() {
  Taro.chooseImage({
    success (res) {
      const tempFilePaths = res.tempFilePaths
      Taro.showLoading({
        title:'添加中'
      })
      Taro.uploadFile({
        url: config.baseUrl + '/mini-program/file/upload',
        filePath: tempFilePaths[0],
        name: 'file',
        header: {
          userId,
          userToken
        },
        success (res){
          const data = JSON.parse(res.data).data.url
          // console.log('data::::', data, typeof data)
          picList.value.push(config.baseUrl+data)
          picArr.push(data)
          watchDisable()
          Taro.hideLoading()
        }
      })
    }
  })
}

function submitFn() {
  const subPicArr = picArr.toString(picArr).split(',').join(';')
  console.log(subPicArr)
  requset('/mini-program/feedback', {
    method:'post',
    data: {
      comment: questionText.value,
      attachments: subPicArr,
      userId,
      userToken
    }
  }).then(res => {
    console.log(res)
    if(res.code === 200) {
      disabled.value = true
      questionText.value = ''
      picList.value = []
      picArr = []
      Taro.showToast({
        title:'上传成功',
        duration: 2000,
      })
      setTimeout(()=>{
        Taro.navigateBack()
      },3000)
    } else {
      Taro.showToast({
        title:'上传失败',
        duration: 1000
      })
    }
    
  })
}

function watchDisable() {
  setTimeout(() => {
    console.log('questionText.value.length::::', questionText.value.length)
    console.log('picList.value.length::::', picList.value.length)
    if( questionText.value.length && picList.value.length ) {
      disabled.value = false
    } else {
      disabled.value = true
    }
  },200)
}

</script>

<style>
.question_container{
  width: 90%;
  margin: 0 auto;
}

.textarea_con{
  width: 100%;
  height: 450px;
  background-color: #FFF;
  border-radius: 20px;
  overflow: hidden;
  padding: 32px;
  box-sizing: border-box;
}

.textarea {
  width: 100%;
  height: 328px;
  border: 0;
  font-size: 30px;
}

.count {
  width: 100%;
  text-align: right;
  display: inline-block;
  margin-top: 20px;
  color: #816747;
}

.uploadPic {
  margin-top: 30px;
}
.uploadPic text:nth-child(1) {
  font-size: 40px;
  color: #3B2122;
}
.uploadPic text:nth-child(2) {
  font-size: 30px;
  color: #816747;
}

.upload_container {
  width: 100%;
  height: 720px;
}

.pic_box{
  width: 320px;
  height: 320px;
  background-color: #FFF;
  margin-bottom: 30px;
  border-radius: 20px;
  overflow: hidden;
  position: relative;
}

.pic_box:nth-child(odd) {
  float: left;
}

.pic_box:nth-child(even) {
  float: right;
}

.upload_pic {
  width: 100%;
  height: 100%;
  
}

.upload_pic_con .del_btn {
  width: 112px;
  height: 72px;
  line-height: 72px;
  color: #FFF;
  text-align: center;
  font-size: 30px;
  position: absolute;
  right: 0;
  bottom: 0;
  background-color: rgba(0,0,0,.5);
  z-index: 10;
  border-top-left-radius: 20px;
}

.click_upload_btn{
  background:#FFF url(../../images/icon_camera.png) no-repeat center 100px;
  background-size: 80px;
  padding-top: 190px;
  box-sizing: border-box;
  color: #816747;
  text-align: center;
  font-size: 36px;
}

</style>